package com.ken.apollo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ken.apollo.domain.entity.MailSendDetail;
import com.ken.apollo.mapper.MailSendDetailMapper;
import com.ken.apollo.service.MailSendDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 邮件发送详情表(MailSendDetail)表服务实现类
 *
 * @author Ken
 * @since 2025-08-25
 */
@Service
@Slf4j
public class MailSendDetailServiceImpl extends ServiceImpl<MailSendDetailMapper, MailSendDetail> implements MailSendDetailService {

    @Override
    public List<MailSendDetail> getDetailsByBatchId(Long batchId) {
        return this.lambdaQuery()
                .eq(MailSendDetail::getBatchId, batchId)
                .orderByAsc(MailSendDetail::getCreateTime)
                .list();
    }

    @Override
    public boolean updateSendStatus(Long detailId, String status, String errorMessage) {
        MailSendDetail detail = new MailSendDetail();
        detail.setId(detailId);
        detail.setSendStatus(status);
        detail.setErrorMessage(errorMessage);
        
        if ("success".equals(status) || "failed".equals(status)) {
            detail.setSentAt(LocalDateTime.now());
        }
        
        return this.updateById(detail);
    }
}